package tournamentTree;

import core.GuiComponentLabels;
import core.Person;
import dataBase.DbHolder;

public class MatchData {
	private int assignedPlayerID;
	private int score;
	private boolean isFreilos;
	private boolean isFinished;
	private boolean isEmpty;
	private boolean isPlayable;

	public enum Position {
		LEFT, RIGHT, NONE
	}

	private Position position;

	public MatchData() {
		this.assignedPlayerID = 0;
		this.score = 0;
		this.isFreilos = false;
		this.isFinished = false;
		this.isEmpty = true;
		this.isPlayable = false;
		this.position = Position.NONE;
	}

	public void setPlayer(int playerID) {
		this.assignedPlayerID = playerID;
		this.isFreilos = false;
		this.isEmpty = false;
	}

	public boolean isFinished() {
		return this.isFinished;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Position getPosition() {
		return this.position;
	}

	public boolean isPlayable() {
		return this.isPlayable;
	}

	public void setPlayable(boolean playable) {
		this.isPlayable = playable;
	}

	public void setFinished(boolean isFinished) {
		this.isFinished = isFinished;
	}

	public boolean isEmpty() {
		return this.isEmpty;
	}

	public boolean isSelectable() {
		return (this.isPlayable() || this.isFinished());
	}

	@Override
	public String toString() {
		if (this.isFreilos) {
			return GuiComponentLabels.FREILOS;

		} else if (this.isFinished) {
			Person toPrint = DbHolder.getInstance().getArcherData().getById(this.assignedPlayerID);
			StringBuilder toReturn = new StringBuilder();
			toReturn.append(toPrint.getName());
			toReturn.append(" ");
			toReturn.append(toPrint.getSurName());
			toReturn.append("\n");
			toReturn.append("(");
			toReturn.append(this.getScore());
			toReturn.append(")");
			return toReturn.toString();

		} else if (this.isEmpty) {
			return GuiComponentLabels.EMPTY;

		} else if (this.isPlayable) {
			Person toPrint = DbHolder.getInstance().getArcherData().getById(this.assignedPlayerID);
			StringBuilder toReturn = new StringBuilder();
			toReturn.append(toPrint.getName());
			toReturn.append(" ");
			toReturn.append(toPrint.getSurName());
			toReturn.append("\n");
			toReturn.append("(");
			if (this.position == Position.LEFT) {
				toReturn.append(GuiComponentLabels.LEFT);
			} else {
				toReturn.append(GuiComponentLabels.RIGHT);
			}
			toReturn.append(")");
			return toReturn.toString();
		}
		/* not yet playable -> 1 opponent missing... */
		else {
			Person toPrint = DbHolder.getInstance().getArcherData().getById(this.assignedPlayerID);
			StringBuilder toReturn = new StringBuilder();
			toReturn.append(toPrint.getName());
			toReturn.append(" ");
			toReturn.append(toPrint.getSurName());
			return toReturn.toString();
		}
	}

	public void setFreilos(boolean isFreilos) {
		this.isFreilos = isFreilos;
		this.isEmpty = false;
	}

	public boolean isFreilos() {
		return this.isFreilos;
	}

	public Integer getAssignedPlayerID() {
		return this.assignedPlayerID;
	}

	public void setScore(int score) {
		this.score = score;
		this.setFinished(true);
	}

	public int getScore() {
		return this.score;
	}
}
